<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
  exit;
}
//ɲ룬£ɾȲˢ
if($continue!="")header("localtion: m_serv.php"); 

$menu_index = 3;
$page_title = 'TABLA';
$guide_message = 'Media | Customer Services';
$out_ary = array();
$ppp = 20;
$page_show = '';

//echo "<pre>";print_r($_POST);
getMyParam('action');



$search_flag = false;
$page_url = 'm_serv.php?';
$sql_where = '';


if ($action != "") {

	if($action=='order'){
	getMyParam('orders');
	getMyParam('order_id');

	for($i=0;$i<count($orders);$i++)
	{
       $sql = "update `med_serv` set orders='{$orders[$i]}' where id={$order_id[$i]} ";
       $db->query($sql);
	}
	header("LOCATION:m_serv.php?continue=true");
	}
	
	
    if ($action == 'add' || $action == 'edit') {
        getMyParam('title'); 
		getMyParam('title_danish'); 
		getMyParam('orders'); 
		getMyParam('msg'); 
		getMyParam('msg_danish'); 

        if ($action == 'add') {
                // Ĳ
                $sql = "INSERT INTO `med_serv` set title='$title',title_danish='$title_danish',
				orders='$orders',msg='$msg',msg_danish='$msg_danish'";
                $db->query($sql);
                //$category_id = $db->insert_id();
				$msg="This serv has been added.";
	            $href="m_serv.php";
				include_once('menu_list.php');
				require_once PrintEot('a_header');
				require_once PrintEot('a_msg');
				require_once PrintEot('a_footer');   
				exit;
        } elseif ($action == 'edit') {
            getMyParam('cid');

            $sql = "update `med_serv` set title='$title',title_danish='$title_danish',
				orders='$orders',msg='$msg',msg_danish='$msg_danish' where id=$cid";
            $db->query($sql);
			$msg="This serv has been updated.";
			$href="m_serv.php";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg');
			require_once PrintEot('a_footer');   
			exit;
        }

    }

    if ($action == 'del') {
        getMyParam('cid');
        if (is_numeric($cid) && $cid > 0) {     
            
            $sql_1 = "DELETE FROM `med_serv` WHERE `id` = '" . $cid . "'";
            $db->query($sql_1);
            
 			$msg="This serv has been deleted. ";
			$href="m_serv.php";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg');
			require_once PrintEot('a_footer');   
			exit;
         }
     }
	
	if ($action == "search") {
	getMyParam('search_key');
    $sql_where .= " where  `title` LIKE '%$search_key%' or `msg` LIKE '%$search_key%'";
    $page_url = 'm_serv.php?action=search&search_key=' . $search_key;
	}
	
}else $sql_where="";



$sql = "SELECT * FROM `med_serv` $sql_where";

$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);



// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
// *** page code end ***
$sql = "SELECT * FROM `med_serv` $sql_where  LIMIT $start_limit, $ppp";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($search_key) != "") {
        $rs['title'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['title']);
		 }
    $out_ary[] = $rs;
}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);
//echo $page_show;
//echo "<pre>";print_r($out_ary);
$debug = 1;
debuginfo();

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_m_serv');
require_once PrintEot('a_footer');

?>